package cn.daomeng.firebird.core.handler;

import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import cn.daomeng.firebird.core.constant.FirebirdContents;
import cn.daomeng.firebird.core.log.LogFactory;

/**
 * 请求链路跟踪拦截器
 * @author wf.shu
 * @since
 */
public class UserRequestHandlerInterceptor implements HandlerInterceptor {
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		//每个请求里添加一个USER_REQUEST_HEAD_ID,作为后续跟踪使用
		request.setAttribute(FirebirdContents.USER_REQUEST_HEAD_ID, UUID.randomUUID());
        LogFactory.info(FirebirdContents.USER_REQUEST_HEAD_ID + " = {}" ,request.getAttribute(FirebirdContents.USER_REQUEST_HEAD_ID));
        return true;
	}

	@Override
	public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
		//doNothing......

	}

	@Override
	public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
		//doNothing......
	}

}
